<?php

namespace Biz\model\MijiaSmartKitchen;

use Biz\pockets\modelTrait\BizModelTrait;
use Doraemon\pockets\datebase\BaseDbModel;

class MijiaSmartKitchenWinningRecord extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_brand_activity';

    const _DATABASE_ = 'brand_activity';

    const _TABLE_NAME = 'mijia_smart_kitchen_winning_record';

    const LOTTERY_PLATFORM_WECHAT = 1;

    use BizModelTrait;

    /**
     * 添加一条中奖记录
     * @param $lotteryId
     * @param $did
     * @param $uid
     * @param $prizeId
     * @param $prizeName
     * @param $winningDate
     * @param $thirdId
     * @param $lotteryPlatform
     * @return false|int
     */
    public static function add($lotteryId, $did, $uid, $prizeId, $prizeName, $winningDate, $thirdId, $lotteryPlatform)
    {
        $data = [
            'lottery_record_id' => $lotteryId,
            'device_id' => $did,
            'uid' => $uid,
            'third_id' => $thirdId,
            'lottery_platform' => $lotteryPlatform,
            'prize_id' => $prizeId,
            'prize_name' => $prizeName,
            'winning_date' => $winningDate,
            'data_status' => self::STATUS_NORMAL,
            'create_time' => date('Y-m-d H:i:s', time()),
            'update_time' => date('Y-m-d H:i:s', time()),
        ];
        return self::dataInsert($data);
    }

    /**
     * 设备是否已存在中奖记录
     * @param $did
     * @return bool
     */
    public static function hasDidWinning($did)
    {
        $where = [
            'device_id' => $did,
            'data_status' => self::STATUS_NORMAL,
        ];

        return self::dataHas($where);
    }

    /**
     * 用户是否已中奖
     * @param $uniq_uid
     * @param $platform
     * @return bool
     */
    public static function hasUserWinning($uniq_uid, $platform)
    {
        $where['data_status'] = self::STATUS_NORMAL;

        if ($platform == self::LOTTERY_PLATFORM_WECHAT) {
            $uid = 0;
            $third_id = $uniq_uid;
            $lottery_latform = 1;
        } else {
            $uid = $uniq_uid;
            $third_id = '';
            $lottery_latform = 0;
        }

        $where['uid'] = $uid;

        $where['third_id'] = $third_id;

        $where['lottery_platform'] = $lottery_latform;

        return self::dataHas($where);
    }

    //今日中奖数量
    public static function todayWinningCount($day)
    {
        $where = [
            'data_status' => self::STATUS_NORMAL,
            'winning_date' => $day
        ];
        return self::dataCount($where);
    }

    /**
     * 用户中奖信息
     * @param $uniq_uid
     * @param $platform
     * @return bool
     */
    public static function getUserWinning($uniq_uid, $platform)
    {
        $where['data_status'] = self::STATUS_NORMAL;

        if ($platform == self::LOTTERY_PLATFORM_WECHAT) {
            $uid = 0;
            $third_id = $uniq_uid;
            $lottery_latform = 1;
        } else {
            $uid = $uniq_uid;
            $third_id = '';
            $lottery_latform = 0;
        }

        $where['uid'] = $uid;

        $where['third_id'] = $third_id;

        $where['lottery_platform'] = $lottery_latform;

        return self::dataSelectInfo($where);
    }

    public static function setDelivery($id, $uname, $phone, $address)
    {
        $update_time = date('Y-m-d H:i:s');
        return self::dataUpdate(compact('uname', 'phone', 'address', 'update_time'), compact('id'));
    }
}